package com.ncs.stm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.ncs.stm.helper.HttpHelper;
import com.ncs.stm.entity.Tip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目选择界面
 *
 *
 */
public class Tips extends Activity {
    private String userid;

    public static List<Map<String,Object>> tipsRemain = null;
    private static Map<String, List<Map<String,Object>>> tipsRemainMap = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tips);
        ListView tipsListView = (ListView)findViewById(R.id.tip_listview);

        userid = getIntent().getStringExtra("userid");

        if (tipsRemainMap == null) {
            tipsRemainMap = new HashMap<String, List<Map<String, Object>>>();
            tipsRemain = new ArrayList<Map<String, Object>>();
            List<Tip> tipList = HttpHelper.getContentListByGetDemo(" http://192.168.82.163/STM/api/CoreValues/GetAvailableValues/",Tip.class);
            for(Tip tip : tipList){
                Map<String,Object> tempMap = new HashMap<String,Object>();
                tempMap.put("tip_desc",tip.getContext());
                tipsRemain.add(tempMap);
            }
            tipsRemainMap.put(userid,tipsRemain);
        }


//        if (tipsRemain == null) {
//            tipsRemain = new ArrayList<Map<String, Object>>();
//            List<Tip> tipList = HttpHelper.getContentListByGetDemo(" http://192.168.82.163/STM/api/CoreValues/GetAvailableValues/",Tip.class);
//            for(Tip tip : tipList){
//                Map<String,Object> tempMap = new HashMap<String,Object>();
//                tempMap.put("tip_desc",tip.getContext());
//                tipsRemain.add(tempMap);
//            }
//        }

        //todo : 需要根据最终显示UI修改显示布局方式
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,tipsRemain,R.layout.tipsitem,
                                                        new String[]{"tip_desc"},new int[]{R.id.tip_desc});

        tipsListView.setAdapter(simpleAdapter);

        tipsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter();
                String surveyResultId =  getIntent().getStringExtra("surveyResultId");
                //获取选中的一整行数据
                Map selectedData = (HashMap)adapterView.getItemAtPosition(i);

                //将选择的题目保存到选择列表中，以便在修改界面显示
                if(SelectedTips.userSurveyResultMap.get(userid) == null){
                    Map<String,List<Map<String,Object>>> surveyResultMap = new HashMap<String, List<Map<String,Object>>>();
                    List<Map<String,Object>> surveyResultList = new ArrayList<Map<String, Object>>();
                    surveyResultList.add(selectedData);
                    surveyResultMap.put(surveyResultId,surveyResultList);
                    SelectedTips.userSurveyResultMap.put(userid,surveyResultMap);
                }else{
                    if(SelectedTips.userSurveyResultMap.get(userid).get(surveyResultId) == null){
                        List<Map<String,Object>> surveyResultList = new ArrayList<Map<String, Object>>();
                        surveyResultList.add(selectedData);
                        SelectedTips.userSurveyResultMap.get(userid).put(surveyResultId,surveyResultList);
                    }else{
                        SelectedTips.userSurveyResultMap.get(userid).get(surveyResultId).add(selectedData);
                    }
                }

                //将选择的题目从题目列表中移除
                tipsRemain.remove(i);
                adapter.notifyDataSetChanged();

                return true;
            }
        });
    }


    public void backToSurvey(View view){
        Intent intent = new Intent(this,SurveyTips.class);
        intent.putExtra("userid",userid);
        startActivity(intent);
    }
}